# coding: utf-8
# 18-11-15 下午8:31
from wechatpy.oauth import WeChatOAuth
from usercenter.models import CustomerProfile

app_id = 'wxe0d95453c412f118'  # 你自己的
secret = 'd785bt925fbc7ebed62734cfdpe5951c'  # 你自己的

redirect_uri = 'http://10.9.141.21::8000/userauth/wechat'


wechatauth = WeChatOAuth(app_id=app_id, secret=secret, redirect_uri=redirect_uri, scope='snsapi_base', state='')


def create_user_from_wx(wx_user_info):
    '''
    创建微信用户
    wx_user_info = {
        "openid":"OPENID",
        "nickname":"NICKNAME",
        "sex":1,
        "province":"PROVINCE",
        "city":"CITY",
        "country":"COUNTRY",
        "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
        "privilege":[
            "PRIVILEGE1",
            "PRIVILEGE2"
        ],
        "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
    }

    '''
    wx_user_info_key_map = {
        "openid": "wechat_openid",
        "nickname": "username",
    }
    user_info = {wx_user_info_key_map[key]: wx_user_info[key] for key in wx_user_info_key_map}
    user_info['wechat_user_info'] = wx_user_info
    return CustomerProfile.objects.update_or_create(defaults=user_info, wechat_openid=user_info['wechat_openid'])
